/*******************************************************************/
/*    Copyright (c) 2008-2010 by Transoft Corp.                    */
/*    All rights reserved.                                         */
/*******************************************************************/

//  : Declaration of the CV5RdWorks

#pragma once
#include "resource.h"       // main symbols

class CBitmapHandler;

class EBomTreeNode;

#define ID_SLDWORKS_EVENTS 0
#define MAIN_CMD_GROUP_ID 0 
#define MAIN_ITEM_ID1 0

#define ParaSolid

//IDL SECTION
// IV5RdWorks
[
	object,
	uuid("15D77B6A-23B5-420A-9767-7C13DEA82074"),
	dual,	
	helpstring("IV5RdWorks Interface"),
	pointer_default(unique)
]
__interface IV5RdWorks : IDispatch
{
	[id(3), helpstring("method ToolbarCallback0")] HRESULT ToolbarCallback0();
	[id(4), helpstring("method ToolbarEnable0")] HRESULT ToolbarEnable0([out, retval] long* status);
	[id(5), helpstring("method OpenV5Part")] HRESULT OpenV5Part([in] BSTR FileName);
	[id(6), helpstring("method OpenV5Product")] HRESULT OpenV5Product([in] BSTR FileName);
#ifdef ParaSolid
	[id(7), helpstring("method OpenV5PartPS")] HRESULT OpenV5PartPS([in] BSTR FileName);
	[id(8), helpstring("method OpenV5ProductPS")] HRESULT OpenV5ProductPS([in] BSTR FileName);
#endif // ParaSolid
};


//C++ HEADER SECTION
// CV5RdWorks

[
	coclass,
	threading(apartment),
	vi_progid("V5RdWorks.V5RdWorks"),
	progid("V5RdWorks.V5RdWorks.1"),
	version(1.0),
	uuid("78312C45-EA6A-41DF-94B0-2A9A16B9EB99"),
	helpstring("V5RdWorks Class")
]

class ATL_NO_VTABLE CV5RdWorks : 
	public IV5RdWorks,
	public ISwAddin,
	public IDispEventImpl<ID_SLDWORKS_EVENTS, CV5RdWorks, &__uuidof(DSldWorksEvents), &LIBID_SldWorks, ID_SLDWORKS_TLB_MAJOR, ID_SLDWORKS_TLB_MINOR>
{
	typedef IDispEventImpl<ID_SLDWORKS_EVENTS, CV5RdWorks, &__uuidof(DSldWorksEvents), &LIBID_SldWorks, ID_SLDWORKS_TLB_MAJOR, ID_SLDWORKS_TLB_MINOR> CSldWorksEvents;

private:
	CComPtr<ISldWorks> iSwApp;
	CComPtr<ICommandManager> iCmdMgr;
	CComObject<CBitmapHandler> *iBmp;
	long addinID;
	long toolbarID;
	long m_swMajNum;
	long m_swMinNum;

	// 8/20/2008 mwu : 
	TCHAR m_strNewOutputDir[MAX_PATH];
	TCHAR m_strNewBaseName[MAX_PATH];
	TCHAR m_strOriginalAssemblyDir[MAX_PATH];
	TCHAR m_strLogFileName[MAX_PATH];

	EBomTreeNode*		m_pRootNode;

public:
	CV5RdWorks()
	{
// 		_tcsset(m_strNewOutputDir,0);
// 		_tcsset(m_strNewBaseName,0);
// 		_tcsset(m_strOriginalAssemblyDir,0);
// 		_tcsset(m_strLogFileName,0);
		m_pRootNode = NULL;
	}

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease() 
	{
	}

	//Utility Methods
public:
	CComPtr<ISldWorks> GetSldWorksPtr() { return iSwApp != NULL ? iSwApp : NULL; }
	//These methods will connect and disconnect this addin to the SolidWorks events
	VARIANT_BOOL AttachEventHandlers();
	VARIANT_BOOL DetachEventHandlers();

	int GetSldWorksTlbMajor() {return (m_swMajNum >= ID_SLDWORKS_TLB_MAJOR ) ? m_swMajNum : 0;}
	int GetSldWorksTlbMinor() {return m_swMinNum;}

	//UI Creation
	//These methods control the User Interface for this addin
	void AddCommandManager();
	void RemoveCommandManager();
	bool CompareIDs(long * storedIDs, long storedSize, long * addinIDs, long addinSize);

	// 6/19/2008 mwu : 
	void AddFileOpenItems();
	void RemoveFileOpenItems();
// 	void AddFileSaveItems();
// 	void RemoveFileSaveItems();
	void CreateDocuments( EBomTreeNode* pTreeNode, int nDepth );
	void ConvertPartFile( BSTR FileName );
	void BuildAssembly( IAssemblyDoc* pAssemblyDoc, int nSize, EBomTreeNode* pTreeNode, int nDepth = 0, bool bOpenPart = false);
	void MergeLogFile( TCHAR* strSourceFileName, TCHAR* strTargetFileName );

	//Event Handlers
	//These are the methods that are called when certain SolidWorks events are fired
	STDMETHOD(OnDocChange)(void);
	STDMETHOD(OnModelDocChange)(void);
	STDMETHOD(OnDocLoad)(BSTR docTitle, BSTR docPath);
	STDMETHOD(OnFileNew)(LPDISPATCH newDoc, long docType, BSTR templateName);
	STDMETHOD(OnFileOpenPostNotify)(BSTR fileName);

	//Event Sinks
	//The SINK_MAP connects the specified SolidWorks event to a specific event handler
	BEGIN_SINK_MAP(CV5RdWorks)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppActiveDocChangeNotify, OnDocChange)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppDocumentLoadNotify, OnDocLoad)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppFileNewNotify2, OnFileNew)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppActiveModelDocChangeNotify, OnModelDocChange)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppFileOpenPostNotify, OnFileOpenPostNotify)
	END_SINK_MAP( )

	// ISwAddin Methods
	//These are the methods inherited from the ISwAddin interface that 
	//need to be implemented in order to connect the addin to SolidWorks
public:
	STDMETHOD(ConnectToSW)(LPDISPATCH ThisSW, long Cookie, VARIANT_BOOL * IsConnected);
	STDMETHOD(DisconnectFromSW)(VARIANT_BOOL * IsDisconnected);

public:
	//UI Callbacks
	//These are the methods that are called by the addin created User Interface
	STDMETHOD(ToolbarCallback0)(void);
	STDMETHOD(ToolbarEnable0)(long* status);
	STDMETHOD(OpenV5Part)(BSTR FileName);
	STDMETHOD(OpenV5Product)(BSTR FileName);

#ifdef ParaSolid
	STDMETHOD(OpenV5PartPS)(BSTR FileName);
	STDMETHOD(OpenV5ProductPS)(BSTR FileName);
#endif // GENERIC

};

